﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class PopArcButton : MonoBehaviour {
    public RectTransform[] buttons;
    public float during=0.5f;
    public float raudia = 100;
    bool isAnim;
    public float initAngle = -180;
    public float angle=30;
    Vector2[] targetPos;
    Vector2 initPos;
    private void Awake()
    {
        targetPos = new Vector2[buttons.Length];
        initPos = buttons[0].anchoredPosition;

    }
    private void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Close();
        }
    }

    private void OnEnable()
    {
        for (int i = 0; i < buttons.Length; i++)
        {
            buttons[i].localScale = Vector3.one;
        }
        StartCoroutine(Pop());
    }
    public void Close()
    {
        StartCoroutine(Push());
    }
    public IEnumerator Pop()
    {
        float a = initAngle;
        for (int i = 0; i < buttons.Length; i++)
        {
            targetPos[i] = initPos + new Vector2(raudia * Mathf.Cos(a), raudia * Mathf.Sin(a));
            a += angle;
        }

        float t = 0;
        
        while (t<during)
        {
            t += Time.deltaTime;
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i].anchoredPosition = Vector2.Lerp(initPos, targetPos[i],t/during);
                buttons[i].localScale = Vector3.Lerp(Vector3.zero, Vector3.one, t / during);
            }
            yield return null;

        }
    }
    public IEnumerator Push()
    {
        yield return null;
        float t = 0;

        while (t < during)
        {
            t += Time.deltaTime;
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i].anchoredPosition = Vector2.Lerp(targetPos[i], initPos, t / during);
                buttons[i].localScale = Vector3.Lerp(Vector3.one, Vector3.zero, t / during);
            }
            yield return null;

        }
        gameObject.SetActive(false);
    }

}
